<?php

namespace Biz\service\course;

use Biz\model\course\Course as mCourse;
use Biz\model\course\CourseOrder as mCourseOrder;
use Doraemon\service\User;
use Doraemon\tools\HhzEnv;

class AdmCourseOrder
{
    public static function getOrderList($where)
    {
        $list = mCourseOrder::selectByWhere($where);
        $uids = array_column($list, 'uid');
        $uids = array_unique($uids);
        $userList = User::getBatchBasicUserInfo($uids);
        $userList = array_column($userList, null, 'uid');
        foreach ($list as &$row) {
            $row['status_desc'] = mCourseOrder::STATUS_DESC[$row['order_status']];
            $row['nick'] = isset($userList[$row['uid']]) ? $userList[$row['uid']]['nick'] : '';
        }
        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = mCourseOrder::selectCount($where);
        return ['list' => $list, 'total' => $count];
    }

    public static function getCourseDaily()
    {
        $courseList = mCourse::selectByWhere([]);
        $courseList = array_column($courseList, null, 'course_id');
        $courseIds = array_keys($courseList);
        if (!HhzEnv::isShopHaoHaoCe()) {
            $courseIds = array_diff($courseIds, [1, 2, 4, 5, 6, 8]);
        }
        $orderList = mCourseOrder::selectByWhere(['course_id' => $courseIds, 'order_status' => mCourseOrder::STATUS_SUCCESS]);
        $courseIds = array_column($orderList, 'course_id');
        $courseIds = array_unique($courseIds);
        $startDate = date('Y-m-d', strtotime('-1 days'));
        $endDate = date('Y-m-d H:i:s', strtotime($startDate) + 86399);
        $totalArr = [];
        foreach ($orderList as $row) {
            $courseId = $row['course_id'];
            $addtime = $row['addtime'];
            if (!isset($totalArr[$courseId]['yesterday_count'])) {
                $totalArr[$courseId]['yesterday_count'] = 0;
            }
            if (!isset($totalArr[$courseId]['yesterday_income'])) {
                $totalArr[$courseId]['yesterday_income'] = 0;
            }
            if (!isset($totalArr[$courseId]['total_count'])) {
                $totalArr[$courseId]['total_count'] = 0;
            }
            if (!isset($totalArr[$courseId]['total_income'])) {
                $totalArr[$courseId]['total_income'] = 0;
            }
            if ($addtime >= $startDate && $addtime <= $endDate) {
                $totalArr[$courseId]['yesterday_count'] = $totalArr[$courseId]['yesterday_count'] + 1;
                $totalArr[$courseId]['yesterday_income'] = bcadd($totalArr[$courseId]['yesterday_income'], $row['buy_price'], 2);
            }
            $totalArr[$courseId]['total_count'] = $totalArr[$courseId]['total_count'] + 1;
            $totalArr[$courseId]['total_income'] = bcadd($totalArr[$courseId]['total_income'], $row['buy_price'], 2);
        }
        $result = [];
        foreach ($courseIds as $courseId) {
            $courseInfo = $courseList[$courseId];
            $info = [
                'course_id' => $courseId,
                'course_title' => $courseInfo['title'],
                'price' => $courseInfo['price'],
                'yesterday_count' => $totalArr[$courseId]['yesterday_count'],
                'yesterday_income' => $totalArr[$courseId]['yesterday_income'],
                'total_count' => $totalArr[$courseId]['total_count'],
                'total_income' => $totalArr[$courseId]['total_income'],
                'online_time' => $courseInfo['online_time'],//上线时间
            ];
            $result[] = $info;
        }
        return $result;
    }

}